package io.nutz.nutzsite.module.acm.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.nutz.nutzsite.module.acm.models.Attend;
import io.nutz.nutzsite.module.acm.services.AttendService;
import io.nutz.nutzsite.common.base.Result;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;

import io.nutz.nutzsite.common.utils.Convert;
import io.nutz.nutzsite.common.utils.ShiroUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Date;
/**
 * acm竞赛提交，最多26题（A-Z） 信息操作处理
 * 
 * @author lrw
 * @date 2020-02-16
 */
@IocBean
@At("/acm/attend")
public class AttendController {
	private static final Log log = Logs.get();

	@Inject
	private AttendService attendService;
	
	@RequiresPermissions("acm:attend:view")
	@At("")
	@Ok("th:/acm/attend/attend.html")
	public void index(HttpServletRequest req) {

	}

	/**
	 * 查询acm竞赛提交，最多26题（A-Z）列表
	 */
	//@RequiresPermissions("acm:attend:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("contestId") String name,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(name)){
			cnd.and("contestId", "=", name );
		}

		return attendService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}

	/**
	 * 新增acm竞赛提交，最多26题（A-Z）
	 */
	@At("/add")
	@Ok("th:/acm/attend/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存acm竞赛提交，最多26题（A-Z）
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:attend:add")
	@Slog(tag="acm竞赛提交，最多26题（A-Z）", after="新增保存acm竞赛提交，最多26题（A-Z） id=${args[0].id}")
	public Object addDo(@Param("..") Attend attend,HttpServletRequest req) {
		try {
			attendService.insert(attend);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	
	@At("/add/?")
	@Ok("json")
	//@RequiresPermissions("acm:attend:add")
	@Slog(tag="增加参赛选手", after="新增参赛选手或考生")
	public Object addDo(String id,HttpServletRequest req) {
		try {
			String uid=ShiroUtils.getSysUserId();
			if(uid==null || uid.length()==0)return Result.error("system.error");
			Integer contestId=Integer.valueOf(id);
			Cnd cnd = Cnd.NEW();
			Attend attend=attendService.fetch(cnd.and("uid","=",uid).and("contestId","=",contestId));
			if (attend == null) {
				attend = new Attend();
				attend.setUid(uid);
				attend.setContestId(contestId);
				attendService.insert(attend);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	/**
	 * 修改acm竞赛提交，最多26题（A-Z）
	 */
	@At("/edit/?")
	@Ok("th://acm/attend/edit.html")
	public void edit(String id, HttpServletRequest req) {
		Attend attend = attendService.fetch(id);
		req.setAttribute("attend",attend);
	}

	/**
	 * 修改保存acm竞赛提交，最多26题（A-Z）
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:attend:edit")
	@Slog(tag="acm竞赛提交，最多26题（A-Z）", after="修改保存acm竞赛提交，最多26题（A-Z）")
	public Object editDo(@Param("..") Attend attend,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(attend)){

				attendService.update(attend);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除acm竞赛提交，最多26题（A-Z）
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("acm:attend:remove")
	@Slog(tag ="acm竞赛提交，最多26题（A-Z）", after= "删除acm竞赛提交，最多26题（A-Z）:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			attendService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
